package com.hyts.bio;

import com.hyts.network.core.Server;
import com.hyts.network.service.bio.BioServer;
import com.hyts.network.task.impl.SocketTaskExecutor;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * project-name:gitee-extends-toolbox
 * package-name:com.hyts.ext.network.demo
 * author:Libo/Alex
 * create-date:2018-10-27 23:06
 * copyright:libo-hyts-github
 * email:libo2dev@aliyun.com
 * description:此类主要用于:
 */
public class BioTestServer extends SocketTaskExecutor {


    static Server connector = null;


    public static void main(String[] args) {
        try {
            ExecutorService excutor = Executors.newFixedThreadPool(4);
            excutor.execute(() -> {
                try {
                    start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            excutor.execute(() -> {
                try {
                    do {
                        if(connector.getInjectResultList()) {
                            System.out.println(connector.resultList);
                        }else{
                            Thread.sleep(1500);
                            System.out.println("正在监控结果列表中......."+System.currentTimeMillis());
                        }
                    } while (!connector.isClosed());
                    System.out.println("监控结束");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            excutor.execute(() -> {
                try {
                    Thread.sleep(20000);
                    connector.stop();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            excutor.shutdown();
        }finally {

        }
    }

    public static void start() throws IOException {
        connector = new BioServer(new ServerSocket(9090));
        connector.start(new BioTestServer());
    }


    @Override
    public Object deal(Object param) {
        return param+"123123123-------------";
    }
}
